import 'dart:convert';
import 'dart:io';
import 'package:amap_flutter_location/amap_flutter_location.dart';
import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:base_flutter/models/index.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_native_splash/flutter_native_splash.dart';
import 'package:logger/logger.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:shared_preferences/shared_preferences.dart';

const _themes = <MaterialColor>[
  Colors.blue,
  Colors.cyan,
  Colors.teal,
  Colors.green,
  Colors.red
];

class Global {

  static Profile profile = Profile();
  // 项目信息
  static bool get isRelease => const bool.fromEnvironment("dart.vm.product");

  static bool get isAndroid => !kIsWeb && Platform.isAndroid;

  static bool get isiOS => !kIsWeb && Platform.isIOS;

  static bool get isOther => !isiOS && !isAndroid;

  static bool get isLastVersion => int.parse(profile.cacheLastVersion) >= int.parse(version.replaceAll('.', ''));

  static bool get isLogin => profile.user != null;
  static String get token => profile.user != null ? profile.user!.token : '';

  static bool get agreeClause => profile.isAgreeClause;

  static String appName = '';
  static String packageName = '';
  static String version = '';
  static String buildNumber = '';
  // 获取设备信息
  static ConnectivityResult netStatus = ConnectivityResult.none;
  static double width = 0;
  static double height = 0;
  // 缓存信息
  static Logger? logger;
  static SharedPreferences? _prefs;

  // 初始化全局信息
  static Future init() async {
    WidgetsBinding widgetsBinding = WidgetsFlutterBinding.ensureInitialized();
    FlutterNativeSplash.preserve(widgetsBinding: widgetsBinding);

    Global.logger = Logger(printer: PrettyPrinter());

    Global.logger?.d('初始化全局信息');
    /// 网络环境
    ///
    Global.netStatus = await (Connectivity().checkConnectivity());
    if (Global.netStatus == ConnectivityResult.mobile) {
      Global.logger?.d('当前是手机网络');
    } else if (Global.netStatus == ConnectivityResult.wifi) {
      Global.logger?.d('当前是WiFi网络');
    }else {
      Global.logger?.d('当前是没有网络');
    }
    /// 软件信息
    ///
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    Global.appName = packageInfo.appName;
    Global.packageName = packageInfo.packageName;
    Global.version = packageInfo.version;
    Global.buildNumber = packageInfo.buildNumber;

    Global.logger?.d('appName:${Global.appName} \npackageName:${Global.packageName}\nversion:${Global.version} \nbuildNumber:${Global.buildNumber}');
    if (kIsWeb) {
      Global.logger?.d('web系统');
    }else {
      Global.logger?.d(Global.isAndroid ? 'Android 系统' : Global.isiOS ? 'iOS 系统' : '其他系统');
    }

    /// Android
    ///
    SystemUiOverlayStyle systemUiOverlayStyle = SystemUiOverlayStyle.dark;
    if (isAndroid) {
      systemUiOverlayStyle = SystemUiOverlayStyle.dark.copyWith(statusBarColor: Colors.transparent);
    }
    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
    /// 持久化信息
    ///
    final prefs = await SharedPreferences.getInstance();
    _prefs = prefs;

    var profileString = prefs.getString('kProfile');
    if (profileString != null) {
      profile = Profile.fromJson(jsonDecode(profileString));
    }else {
      profile = Profile()..cacheLastVersion = '0'..isAgreeClause = false;
    }
    if (profile.user != null) {
      logger?.d("当前登录用户信息: ${profile.user?.toJson()}");
    }

    /// 初始化网络配置
    ///

    /// 定位
    ///

    logger?.d("初始化结束");

  }
  static settingAmapKey() {
    AMapFlutterLocation.setApiKey('2303616f18bc17024c9bd159463c7498', '636f4dd976f11f267d9f1a24c0dcaf6c');
  }
  // 持久化Profile信息
  static saveProfile() async {
    await _prefs?.setString("kProfile", jsonEncode(profile.toJson()));
  }

}
void debugInfo(dynamic info) => Global.logger?.d(info);